/*
 * BitPanel.java
 *
 * Created on 7. zari 2007, 17:25
 */

package robotour.hardware.oldmotors;

import javax.swing.JCheckBox;
import javax.swing.event.ChangeListener;

/**
 *
 * @author  PC
 */
class BitPanel extends javax.swing.JFrame implements ChangeListener {
    
        
    
    /** Creates new form BitPanel */
    public BitPanel() {
        initComponents();
        bits[0] = jCheckBox1;
        bits[1] = jCheckBox2;
        bits[2] = jCheckBox3;
        bits[3] = jCheckBox4;
        bits[4] = jCheckBox5;
        bits[5] = jCheckBox6;
        bits[6] = jCheckBox7;
        bits[7] = jCheckBox8;
        
        for (int i = 0; i < 8; i++) {
            bits[i].setText(KoscaksPatent.INSTANCE.getCaption(i));
            bits[i].addChangeListener(this);
        }
        
    }
    
    
    JCheckBox[] bits = new JCheckBox[8];
    
    public void stateChanged(javax.swing.event.ChangeEvent evt) {
        int value = 0;
        
        for (int i = 0; i < 8; i++) {
            value += ((bits[i].isSelected())?1:0) << i;
        }
        
        valueTF.setText(""+value);
        
//        float rsum = 0;
//        float rmul = 1;
//
//        for (int i = 0; i < 8; i++) {
//            if (bits[i].isSelected()) {
//                rmul *= rezs[i];
//            }
//        }
//
//        for (int i = 0; i < 8; i++) {
//            if (bits[i].isSelected()) {
//                rsum += rmul/rezs[i];
//            }
//        }  R=(rmul/rsum)
        
        rezistenceLabel.setText(""+(KoscaksPatent.INSTANCE.getRezistence(value)/1000.0)+"k");
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jCheckBox3 = new javax.swing.JCheckBox();
        jCheckBox4 = new javax.swing.JCheckBox();
        jCheckBox5 = new javax.swing.JCheckBox();
        jCheckBox6 = new javax.swing.JCheckBox();
        jCheckBox7 = new javax.swing.JCheckBox();
        jCheckBox8 = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        rezistenceLabel = new javax.swing.JLabel();
        valueTF = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("BitPanel");

        jCheckBox1.setText("jCheckBox1");
        jCheckBox1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));

        jCheckBox2.setText("jCheckBox2");
        jCheckBox2.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));

        jCheckBox3.setText("jCheckBox3");
        jCheckBox3.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));

        jCheckBox4.setText("jCheckBox4");
        jCheckBox4.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));

        jCheckBox5.setText("jCheckBox5");
        jCheckBox5.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));

        jCheckBox6.setText("jCheckBox6");
        jCheckBox6.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));

        jCheckBox7.setText("jCheckBox7");
        jCheckBox7.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));

        jCheckBox8.setText("jCheckBox8");
        jCheckBox8.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));

        jLabel1.setText("rezistence");

        jLabel2.setText("value");

        rezistenceLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        rezistenceLabel.setText("0");

        valueTF.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        valueTF.setText("0");
        valueTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                valueTFActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox1)
                    .addComponent(jCheckBox2)
                    .addComponent(jCheckBox3)
                    .addComponent(jCheckBox4)
                    .addComponent(jCheckBox5)
                    .addComponent(jCheckBox6)
                    .addComponent(jCheckBox7)
                    .addComponent(jCheckBox8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(valueTF)
                    .addComponent(rezistenceLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBox1)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBox2)
                    .addComponent(rezistenceLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBox5)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBox6)
                    .addComponent(valueTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox8)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
        
    private void valueTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_valueTFActionPerformed
        int value = Integer.parseInt(valueTF.getText());
        for (int i = 0; i < 8; i++) {
            bits[i].setSelected((value&(1 << i))!=0);
        }
        rezistenceLabel.setText(""+(int)(KoscaksPatent.INSTANCE.getRezistence(value)/1000)+"k");
    }//GEN-LAST:event_valueTFActionPerformed
        
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BitPanel().setVisible(true);
            }
        });
        System.out.println("bits\trezistence");
        for (int i = 0; i < 256; i++) {
            System.out.println(""+i+"\t"+KoscaksPatent.INSTANCE.getRezistence(i));
        }
        System.out.println("ordered by rezistence");
        System.out.println("rezistence\tbits");        
        for (Float float1 : KoscaksPatent.INSTANCE.createTable().keySet()) {
            System.out.println(""+float1+"\t"+KoscaksPatent.INSTANCE.createTable().get(float1));
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JCheckBox jCheckBox4;
    private javax.swing.JCheckBox jCheckBox5;
    private javax.swing.JCheckBox jCheckBox6;
    private javax.swing.JCheckBox jCheckBox7;
    private javax.swing.JCheckBox jCheckBox8;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel rezistenceLabel;
    private javax.swing.JTextField valueTF;
    // End of variables declaration//GEN-END:variables
    
}
